<template>
  <div class="login">
    <!-- 背景系统名称 -->
    <div class="background-title">
      <h1 class="main-title">抖音商品评论分析系统</h1>
      <p class="subtitle">专业数据统计 · 智能评论挖掘 · 精准决策支持</p>
    </div>
    
    <!-- 登录表单 -->
    <div class="login-box">
      <div class="login-header">
        <h2 class="login-title">系统登录</h2>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="用户名"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 底部功能区 -->
    <div class="login-footer">
      <div class="footer-functions">
        <span class="function-item">使用帮助</span>
        <span class="function-item">隐私政策</span>
        <span class="function-item">联系我们</span>
      </div>
      <div class="copyright">
        <span>Copyright © 2018-2025 {{title}} All Rights Reserved.</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}

/* 背景遮罩 */
.login::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 0;
}

/* 背景系统名称 */
.background-title {
  position: absolute;
  top: 10%;
  left: 0;
  width: 100%;
  text-align: center;
  color: white;
  z-index: 1;
}

.main-title {
  font-size: 36px;
  font-weight: 600;
  margin: 0 0 10px 0;
  letter-spacing: 2px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.subtitle {
  font-size: 16px;
  margin: 0;
  opacity: 0.9;
  letter-spacing: 1px;
}

/* 登录表单 */
.login-box {
  width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  z-index: 2;
  margin-bottom: 100px;
}

.login-header {
  text-align: center;
  padding: 25px 0 15px 0;
  border-bottom: 1px solid #eaeaea;
}

.login-title {
  font-size: 20px;
  font-weight: 500;
  color: #333;
  margin: 0;
}

.login-form {
  padding: 30px;
  .el-input {
    height: 40px;
    margin-bottom: 20px;
    input {
      height: 40px;
      border-radius: 4px;
      border: 1px solid #ddd;
    }
    input:focus {
      border-color: #409eff;
    }
  }
  .input-icon {
    height: 40px;
    width: 16px;
    margin-left: 2px;
    color: #999;
  }
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    border-radius: 4px;
  }
}

.login-code-img {
  height: 40px;
  width: 100%;
  object-fit: cover;
}

/* 底部功能区 */
.login-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  text-align: center;
  padding: 20px 0;
  color: rgba(255, 255, 255, 0.8);
  z-index: 1;
}

.footer-functions {
  margin-bottom: 10px;
}

.function-item {
  margin: 0 15px;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.3s;
}

.function-item:hover {
  color: white;
}

.copyright {
  font-size: 12px;
  opacity: 0.7;
}

/* 登录按钮样式调整 */
.el-button--primary {
  background-color: #1890ff;
  border-color: #1890ff;
  font-size: 16px;
  height: 40px;
}

.el-button--primary:hover,
.el-button--primary:focus {
  background-color: #40a9ff;
  border-color: #40a9ff;
}

/* 记住密码样式 */
.el-checkbox__inner {
  border-color: #ddd;
}

.el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #1890ff;
  border-color: #1890ff;
}
</style>
